package com.sheep.jucdemo.part11.part11_5;

import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Date 2022/5/26 22:30
 * @Created by sheep
 */
public class Test11_5_2 {
    // 创建定时器对象
    static ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);

    public static void main(String[] args) {
        // 添加任务1，延迟500ms执行
        scheduledThreadPoolExecutor.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("---one Task---");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                throw new RuntimeException("error");
            }
        }, 500, TimeUnit.MILLISECONDS);

        // 添加任务2，延迟1000ms执行
        scheduledThreadPoolExecutor.schedule(new TimerTask() {
            @Override
            public void run() {
                for (;;){
                    System.out.println("---two Task---");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, 1000, TimeUnit.MILLISECONDS);
    }
}
